<template>
  <div class="search-condition">
    <div class="header">
      <div>
        <el-input
          style="width: 300px;margin-right: 12px"
          v-model="searchWord"
          placeholder="请输入关键字"
          :suffix-icon="Search"
          size="large"
        />
        <el-button plain @click="onDoSearch" size="large" type="primary">搜索</el-button>
        <el-button plain @click="onDoReset" size="large" type="primary">重置</el-button>
      </div>
      <slot name="operation"></slot>
    </div>
    <div class="tag-list">
      <div v-if="isIndustrySectorShow" class="tag-row">
        <div class="tag-title">所属行业</div>
        <div class="tags">
          <div
            @click="onIndustryTagClick(-1)"
            :class="{ 'tag-active': -1 === industryActiveTagIndex }"
            class="tag c-p"
          >
            全部
          </div>
          <div
            v-for="(item, index) in industrySectorList"
            @click="onIndustryTagClick(index)"
            :class="{ 'tag-active': index === industryActiveTagIndex }"
            :key="item.label"
            class="tag c-p"
          >
            {{ item.label }}
          </div>
        </div>
      </div>
      <div v-if="isLocationShow" class="tag-row">
        <div class="tag-title">所在地</div>
        <div class="tags">
          <div
            v-for="(item, index) in provinceList"
            @click="onlocationTagClick(index)"
            :class="{ 'tag-active': index === locationActiveTagIndex }"
            :key="item.label"
            class="tag c-p"
          >
            {{ item.label }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Search } from '@element-plus/icons-vue';
import { industrySectorList } from '@/data/industrySector';
import { provinceList } from '@/data/province';

const searchWord = ref<string>('');
const emit = defineEmits(['doSearch']);
const props = defineProps<{
  isIndustrySectorShow?: boolean;
  isLocationShow?: boolean;
}>();

// 行业领域
const industryActiveTagIndex = ref(-1);
function onIndustryTagClick(index: number) {
  industryActiveTagIndex.value = index;
  emitEvent();
}

// 所在地
const locationActiveTagIndex = ref(0);
function onlocationTagClick(index: number) {
  locationActiveTagIndex.value = index;
  emitEvent();
}

function onDoSearch() {
  emitEvent();
}
function onDoReset() {
  searchWord.value = '';
  industryActiveTagIndex.value = -1;
  locationActiveTagIndex.value = 0;
  emitEvent();
}

// 触发事件
function emitEvent() {
  let industrySector = null;
  if (industryActiveTagIndex.value !== -1) {
    industrySector = industrySectorList[industryActiveTagIndex.value].value;
  }
  emit('doSearch', {
    searchWord: searchWord.value,
    industrySector,
    location: provinceList[locationActiveTagIndex.value].value,
  });
}
</script>

<style lang="scss" scoped>
.search-condition {
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0px 50px 20px 100px;
  }
  .tag-list {
    .tag-row {
      display: flex;
      flex-wrap: wrap;
      .tag-title {
        padding: 5px;
        width: 100px;
        flex-shrink: 0;
        text-align: center;
      }
      .tags {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        .tag {
          padding: 5px;
          margin: 0 10px 10px 0;
        }
        .tag-active {
          border: 2px solid #63b0ff;
          background-color: #ecf5ff;
        }
      }
    }
  }
}
</style>
